//
//  GFCommentResturtantViewController.m
//  GoodFood
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFCommentResturtantViewController.h"

@interface GFCommentResturtantViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, copy) NSString *placeId;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) NSMutableArray *commentArray;
@property (nonatomic, strong) UITableView *commentTable;
@end

@implementation GFCommentResturtantViewController
#pragma mark - 初始化
- (instancetype)initWithPlaceId:(NSString *)placeId {
    self = [super init];
    if (self) {
        self.placeId = placeId;
        self.page = 1;
    }
    return self;
}
#pragma mark - 导航
- (void)viewForNavi {
    UIView *view = (UIView *)[self.navigationController.view viewWithTag:10001];
    view.backgroundColor = [UIColor redColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth / 2, ScreenHeight / 3, ScreenWidth / 3, ScreenHeight / 4)];
    label.text = @"全部评论";
    label.textColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.7];
    label.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = label;
    self.view.backgroundColor = [UIColor lightGrayColor];
}
#pragma mark - 数据加载
- (void)handleData {
    NSString *url = [NSString stringWithFormat:@"http://api.meishixing.com/picture/picturelist/comment/place_id=%@&page=%ld", self.placeId, self.page];
    if (1 == self.page) {
        self.commentArray = [NSMutableArray array];
    }
    [AFNetHandle GETWithURL:url body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        NSDictionary *dictionary = [NSDictionary dictionaryWithDictionary:result];
        NSArray *arr = [dictionary objectForKey:@"result"];
        for (NSDictionary *dic in arr) {
            GFCommentResturtantModel *model = [GFCommentResturtantModel modWithDic:dic];
            [self.commentArray addObject:model];
        }
        if (self.commentArray.count != 0) {
            [self.commentTable reloadData];
        }
        if (arr.count != 0) {
            if (self.commentArray.count < 20) {
                self.commentTable.footer.hidden = YES;
            } else {
                self.commentTable.footer.hidden = NO;
            }
        } else {
            self.commentTable.footer.hidden = YES;
        }
        [self.waitView removeFromSuperview];
        [self.overTimeView removeFromSuperview];
    } fail:^(NSError *error) {
        [self.overTimeView removeFromSuperview];
        self.overTimeView = [[GFOverTimeView alloc] initWithFrame:self.view.frame withButtonBlock:^(UIButton *button) {
            [self.view addSubview:self.waitView];
            [self handleData];
        }];
        [self.waitView removeFromSuperview];
        [self.view addSubview:self.overTimeView];
    }];
}
#pragma mark - 创建tableView
- (void)createTableView {
    self.commentTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
    self.commentTable.delegate = self;
    self.commentTable.dataSource = self;
    [self.commentTable registerClass:[GFDetialCommentResturtantTableViewCell class] forCellReuseIdentifier:@"comments"];
    self.commentTable.tableFooterView = [[UIView alloc] init];
    self.commentTable.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refresh:)];
    self.commentTable.footer.hidden = YES;
    [self.view addSubview:self.commentTable];
}
#pragma mark - 上拉加载
- (void)refresh:(MJRefreshFooter *)footer {
    [self.view addSubview:self.waitView];
    self.page ++;
    [self.commentTable.footer beginRefreshing];
    [self.commentTable.footer endRefreshing];
    [self handleData];
}
#pragma mark - row number
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.commentArray.count;
}
#pragma mark - cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GFDetialCommentResturtantTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"comments"];
    cell.model = [self.commentArray objectAtIndex:indexPath.row];
    return cell;
}
#pragma mark - row height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [GFDetialCommentResturtantTableViewCell heightForCellWithComment:[[self.commentArray objectAtIndex:indexPath.row] comment]];
}
#pragma mark - 视图加载
- (void)viewDidLoad {
    [super viewDidLoad];
    [self viewForNavi];
    [self createTableView];
    [self.view bringSubviewToFront:self.waitView];
    [self handleData];
}
@end
